package com.atguigu.gulimail.coupon.service.impl;

import com.atguigu.common.to.MemberPriceTo;
import com.atguigu.common.to.SkuReductionTo;
import com.atguigu.gulimail.coupon.entity.MemberPriceEntity;
import com.atguigu.gulimail.coupon.entity.SkuLadderEntity;
import com.atguigu.gulimail.coupon.service.MemberPriceService;
import com.atguigu.gulimail.coupon.service.SkuLadderService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimail.coupon.dao.SkuFullReductionDao;
import com.atguigu.gulimail.coupon.entity.SkuFullReductionEntity;
import com.atguigu.gulimail.coupon.service.SkuFullReductionService;


@Service("skuFullReductionService")
public class SkuFullReductionServiceImpl extends ServiceImpl<SkuFullReductionDao, SkuFullReductionEntity> implements SkuFullReductionService {


    @Autowired
    SkuLadderService skuLadderService;
    @Autowired
    MemberPriceService memberPriceService;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuFullReductionEntity> page = this.page(
                new Query<SkuFullReductionEntity>().getPage(params),
                new QueryWrapper<SkuFullReductionEntity>()
        );

        return new PageUtils(page);
    }

    //把满减的To对象保存到数据库中
    @Override
    public void saveSkuReduction(SkuReductionTo skuReductionTo) {
        //1保存优惠信息
        //创建skuLadderEntity 优惠实体  作用对应数据库打折实体
        SkuLadderEntity skuLadderEntity = new SkuLadderEntity();
        //设置skuId 对应具体商品
        skuLadderEntity.setSkuId(skuReductionTo.getSkuId());
        //设置满几件
        skuLadderEntity.setFullCount(skuReductionTo.getFullCount());
        //设置打几折
        skuLadderEntity.setDiscount(skuReductionTo.getDiscount());
        //设置叠加优惠
        skuLadderEntity.setAddOther(skuReductionTo.getCountStatus());
        if(skuReductionTo.getFullCount() >0){
            //把To对象保存到数据库
            skuLadderService.save(skuLadderEntity);
        }



        //保存满减信息
        //创建满减实体对象
        SkuFullReductionEntity skuFullReductionEntity = new SkuFullReductionEntity();
        //使用BeanUtils工具类进行属性赋值
        BeanUtils.copyProperties(skuReductionTo, skuFullReductionEntity);
        if(skuFullReductionEntity.getFullPrice().compareTo(new BigDecimal("0")) ==1){
            //保存到数据库中
            this.save(skuFullReductionEntity);
        }

        //保存会员价格
        //product服务传递过来的To 中获取会员价格实体
        List<MemberPriceTo> memberPrice = skuReductionTo.getMemberPrice();
        //流式处理会员价格的
        List<MemberPriceEntity> collect = memberPrice.stream().map(item -> {
            //创建一个会员价格实体
            MemberPriceEntity priceEntity = new MemberPriceEntity();

            //设置skuId
            priceEntity.setSkuId(skuReductionTo.getSkuId());
            //设置会员价格等级
            priceEntity.setMemberLevelId(item.getId());
            //设置等级名称
            priceEntity.setMemberLevelName(item.getName());
            priceEntity.setAddOther(1);
            return priceEntity;
        }).filter(
                item ->{
                    return item.getMemberPrice().compareTo(new BigDecimal("0")) ==1;
                }
        ).collect(Collectors.toList());
        //保存到数据库
        memberPriceService.saveBatch(collect);
    }

}